#!/usr/bin/env python3
"""
Launcher script for plotting PCA embeddings.

This is a thin wrapper that imports and calls the visualization function from
src.connecting_people.viz.pca_plots.
"""
import argparse
import sys
from pathlib import Path

# Add the src directory to the Python path to enable imports
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))

from connecting_people.viz.pca_plots import plot_centuries_and_persons


def main():
    """Parse command-line arguments and plot PCA embeddings."""
    parser = argparse.ArgumentParser(
        description="Plot century and person embeddings in 3D PCA space."
    )
    parser.add_argument(
        "--century-embeddings",
        type=str,
        required=True,
        help="Path to pickled dictionary of century embeddings"
    )
    parser.add_argument(
        "--person-embeddings",
        type=str,
        required=True,
        help="Path to pickled dictionary of person embeddings"
    )
    parser.add_argument(
        "--field-of-activity",
        type=str,
        required=True,
        help="Field of human activity (e.g., 'Science', 'Politics')"
    )
    parser.add_argument(
        "--persons-to-plot",
        type=str,
        nargs="*",
        default=None,
        help="Names of specific persons to plot (optional)"
    )
    parser.add_argument(
        "--no-save",
        action="store_true",
        help="Don't save the plot to disk"
    )
    parser.add_argument(
        "--no-summary",
        action="store_true",
        help="Don't print summary of nearest centuries"
    )

    args = parser.parse_args()

    # Load embeddings (user should provide these as pickle files or implement loading)
    import pickle
    
    with open(args.century_embeddings, "rb") as f:
        century_embeddings = pickle.load(f)
    
    with open(args.person_embeddings, "rb") as f:
        person_embeddings = pickle.load(f)

    # Call the plotting function
    result = plot_centuries_and_persons(
        century_embeddings=century_embeddings,
        person_embeddings=person_embeddings,
        field_of_activity=args.field_of_activity,
        persons_to_plot=args.persons_to_plot,
        save_plot=not args.no_save,
        print_summary=not args.no_summary,
    )

    print(f"Plot completed for {args.field_of_activity}")
    if result and "explained_variance" in result:
        print(f"Explained variance (PC1-3): {result['explained_variance']}")


if __name__ == "__main__":
    main()
